<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Wine List Code Gen</title>

    <script type="text/javascript">
    	//<![CDATA[
function el(id) {
  return document.getElementById(id);
}

function synchronousGetText(url) {
  var req = new XMLHttpRequest();
  req.open('GET', url, false);
  req.send(null);
  if(!(req.status == 200 || req.status == 0) || req.responseText == '')
    return null;
  else
    return req.responseText;
}

function addGetTextFunctionToElement(targetElement, documentText, functionName) {
  var targetDocument,
      documentLength,
      variableName,
      doubleIndent,
      trippleIndent,
      maxLineLength,
      stringReplaceRegex,
      segment,
      lineEndingIndex,
      segmentLineEnding,
      segmentLineOpening,
      addLine,
      i,
      nextSingleQuote,
      nextBackslash,
      nextNewLine,
      nextCloseTag,
      nextMatch;
      
  targetDocument = targetElement.ownerDocument;
  documentLength = documentText.length;
  variableName = 't';
  maxLineLength = 80;

  addLine = function (text) {
    targetElement.appendChild(targetDocument.createTextNode(text));      
    targetElement.appendChild(targetDocument.createElement('br'));
  };
  
  doubleIndent = "\xA0\xA0\xA0\xA0";
  trippleIndent = "\xA0\xA0\xA0\xA0\xA0\xA0";
  segmentLineEnding = "';";
  segmentLineOpening = trippleIndent + variableName + " = " + variableName + " + '";
  stringReplaceRegex = /[ \t\n][ \t\n]+/g;
  
  addLine(doubleIndent + "function " + functionName + "() {");
  addLine(trippleIndent + "var " + variableName + ";");
  
  segment = trippleIndent + variableName + " = '";
  
  for (i = 0; i < documentLength; ) {
    nextMatch = documentLength;
    
    nextSingleQuote = documentText.indexOf("'", i);
    if (nextSingleQuote > -1) {
      nextMatch = Math.min(nextMatch, nextSingleQuote);
    }
    
    nextBackslash = documentText.indexOf("\\", i);
    if (nextBackslash > -1) {
      nextMatch = Math.min(nextMatch, nextBackslash);
    }
    
    nextNewLine = documentText.indexOf("\n", i);
    if (nextNewLine > -1) {
      nextMatch = Math.min(nextMatch, nextNewLine);
    }
    
    nextCloseTag = documentText.indexOf("<\/", i);
    if (nextCloseTag > -1) {
      nextMatch = Math.min(nextMatch, nextCloseTag);
    }
    
    segment = segment + documentText.substring(i, nextMatch);
    segment = segment.replace(stringReplaceRegex, " ");
    switch (nextMatch) {
      case nextSingleQuote:
        segment = segment + "\\\'";
        i = nextMatch + 1;
        break;
      
      case nextBackslash:
        segment = segment + "\\\\";
        i = nextMatch + 1;
        break;
      
      case nextNewLine:
        i = nextMatch + 1;
        break;
      
      case nextCloseTag:
        segment = segment + "<\\\/";
        i = nextCloseTag + 2;
        break;
    }
    
    while((segment.length + segmentLineEnding.length) >= maxLineLength) {
      lineEndingIndex = maxLineLength - segmentLineEnding.length;
      while (segment.charAt(lineEndingIndex - 1) == "\\") {
        lineEndingIndex -= 1;
      }
      addLine(segment.substring(0, lineEndingIndex) + segmentLineEnding);
      segment = segmentLineOpening + segment.substring(lineEndingIndex);
    }
  }
  
  addLine(segment + segmentLineEnding);
  addLine(trippleIndent + "return " + variableName + ";");
  addLine(doubleIndent + "}");
  targetElement.appendChild(targetDocument.createElement('br'));
}

function generateWineListCode() {
  var dataCleanupURL = 'transform/pretransform.xslt';
  var dataPresentationURL = 'transform/transform.xslt';

  var dataCleanupText = synchronousGetText(dataCleanupURL);
  if(dataCleanupText === null) {
    el('htmldisplay').innerHTML = "Error loading data cleanup transform";
    return;
  };

  var dataPresentationText = synchronousGetText(dataPresentationURL);
  if(dataPresentationText === null) {
    el('htmldisplay').innerHTML = "Error loading data presentation transform";
    return;
  };

  var targetElement = el('htmldisplay');  
  
  addGetTextFunctionToElement(targetElement, dataCleanupText, 'getDataCleanupText');
  addGetTextFunctionToElement(targetElement, dataPresentationText, 'getDataPresentationText');
}
		//]]>
    </script>
  </head>
  <body onload="generateWineListCode()">
    <div id="htmldisplay"></div>
  </body>
</html>
